const tagsView = {
  state: {
    visitedViews: [],
    cachedViews: [
      // 'takeCarOrderWrap',
      // 'dingding',
      // 'vehicleMaintain',
      // 'drivingLicense',
      // 'tradingCard',
      // 'violation',
      // 'vehicleMaintain',
      // 'businessNeeds',
      // 'returnCarOrderWrap',
      // 'takeCarOrderWrap',
      // 'allotDemandSheetWrap',
      // 'oneKeyReport'
    ],
    refreshTime: '',
    activeAnum: '1',
  },
  mutations: {
    ADD_VISITED_VIEWS: (state, view) => {
      state.visitedViews.forEach(item => {
        if (item.path === view.path) {
          // Object.keys(item.query).forEach((items) => {
          //     if (item.query[items] != view.query[items]) {
          //         // item.query[items] = view.query[items]
          //         // console.log(item.query[items], 'ddddd')
          //         item.query = [
          //             ...view.query
          //         ]
          //     }
          // })
          item.query = {
            ...view.query
          }
        }
      })
      if (state.visitedViews.some(v => v.path === view.path)) return
      state.visitedViews.push(
        Object.assign({}, view, {
          title: view.meta.title || 'no-name'
        })
      )
      if (!view.meta.noCache) {
        state.cachedViews.push(view.name)
      }
    },
    DEL_VISITED_VIEWS: (state, view) => {
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path) {
          state.visitedViews.splice(i, 1)
          break
        }
      }
      for (const i of state.cachedViews) {
        if (i === view.name) {
          const index = state.cachedViews.indexOf(i)
          state.cachedViews.splice(index, 1)
          break
        }
      }
    },
    DEL_OTHERS_VIEWS: (state, view) => {
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path) {
          state.visitedViews = state.visitedViews.slice(i, i + 1)
          break
        }
      }
      for (const i of state.cachedViews) {
        if (i === view.name) {
          const index = state.cachedViews.indexOf(i)
          state.cachedViews = state.cachedViews.slice(index, index + 1)
          break
        }
      }
    },
    DEL_ALL_VIEWS: state => {
      state.visitedViews = []
      state.cachedViews = []
    },
    EDIT_VISITED_VIEWS: (state, view) => {
      for (var Index in state.visitedViews) {
        if (state.visitedViews[Index].path == view.path) {
          state.visitedViews[Index].title = view.meta.title
        }
      }
    },
    REFRESH_TIME: (state) => {
      state.refreshTime = (new Date()).valueOf()
    },
    AMEND_ACTIVE: (state, view) => {
      state.activeAnum = view
    }
  },
  actions: {
    addVisitedViews ({
      commit
    }, view) {
      commit('ADD_VISITED_VIEWS', view)
    },
    delVisitedViews ({
      commit,
      state
    }, view) {
      return new Promise(resolve => {
        commit('DEL_VISITED_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    delOthersViews ({
      commit,
      state
    }, view) {
      return new Promise(resolve => {
        commit('DEL_OTHERS_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    delAllViews ({
      commit,
      state
    }) {
      return new Promise(resolve => {
        commit('DEL_ALL_VIEWS')
        resolve([...state.visitedViews])
      })
    },
    editVisitedViews ({
      commit,
      state
    }, view) {
      return new Promise((resolve) => {
        commit('EDIT_VISITED_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    refreshTime ({
      commit,
      state
    }, view) {
      return new Promise((resolve) => {
        commit('REFRESH_TIME')
      })
    },
    amendactive ({
      commit,
      state
    }, view) {
      return new Promise((resolve) => {
        commit('AMEND_ACTIVE', view)
        resolve([...state.activeAnum])
      })
    },
  }
}

export default tagsView
